﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KeanuMvc.Reference;
using HIS.Utils;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Configuration;
using HIS.Utils.Securities;

namespace HIS.Controllers.Master
{
    public class ItemLocations : HIS.Controllers.AppBaseController
    {

        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private ItemLocation itemloc = null;
        #endregion

       
        #region Items Locations

        public ItemLocations()
        {
            Data.Add("Pagetitle", "Item Locations");
            Data.Add("Notification", strNotification);
        }

        [Authorization(RoleAction = "master-items")]
        public override void Index()
        {
            try
            {
                Data.Add("siteId", HttpContext.Current.Request["siteid"]);
                Data["Pagetitle"] += " :: List Item Location ::";
                RenderFile("master\\items\\itemlocations.html");
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-items")]
        public void TableList()
        {
            try
            {

                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];               
                string namevalue = HttpContext.Current.Request["namevalue"].NullSafeToString();
                string siteid = HttpContext.Current.Request["siteid"].NullSafeToString();

                string ssearch = "nama=" + namevalue + ";siteid=" + siteid;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                //pagingSearch.sSearch = HttpContext.Current.Request["sSearch"];
                if (!string.IsNullOrEmpty(ssearch))
                {
                    pagingSearch.sSearch = ssearch;
                }
                else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }
                pagingSearch.sSearch = ssearch;

                string strUri = string.Empty;
                strUri = Global.ServiceUriDictionary["itemlocationpaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));

                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-items")]
        //edit oke
        public void EditItemLocation()
        {
            StringReader stringReader = null;
            /// Get id record from querystring
            int id = 0;
            try
            {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            }
            catch
            {
                /// Throw to list if something goes wrong
                GoToUrl("itemlocation/"); return;
            }

            try
            {
                ///Set Rest Url///
                string strUri = Global.ServiceUriDictionary["itemlocation"] + id + Global.ServiceUriDictionary["requestformat"];
                ///Get Rest///
                stringReader = base.RestService.GetJson(strUri);
                ///Deserialize json response to object///
                itemloc= JsonConvert.DeserializeObject<ItemLocation>(stringReader.ReadToEnd());
                ///Set variable for rendering on view HTML///
                Data["Pagetitle"] += " :: Edit Item Shapes ::";
                Data.Add("refItemShape", itemloc);
                Data.Add("IsCreate", "false");
                Data.Add("Actionform", Data["rooturl"] + "master/itemlocation/editlocation/");
                Data["Notification"] = strNotification;
            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {
                RenderFile("Master\\itemgroups\\edititemlocation.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-items")]
        //Delete belum
        public void DeleteItemLocation()
        {
            StringReader stringReader = null;
            int id = 0;
            try
            {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            }
            catch
            {
                GoToUrl("itemlocation/");
                return;
            }

            try
            {
                itemloc = new HISModels.Entities.ItemLocation();
                string strUri = Global.ServiceUriDictionary["itemlocation"] + Global.ServiceUriDictionary["requestformat"];

                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));
                var a = JsonConvert.DeserializeObject<RefItemShape>(stringReader.ReadToEnd());
                if (strNotification == string.Empty) strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                Data["Notification"] = strNotification;

            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {
                //RenderFile("Admins\\Actions\\editgroupspecialist.html");
                //stringReader.Dispose();
                //base.RestService.Dispose();


                HttpContext.Current.Response.Redirect(Data["rooturl"] + "master/itemlocation/");

                //GoToUrl("groupspecialist/");

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-items")]
        //save oke
        public void SaveItemShapes()
        {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try
            {
                itemloc = new ItemLocation(); Utils.Helper.SetNullToEmptyString(itemloc);
                if (HttpContext.Current.Request["IsCreate"] != "true")
                {
                    int id = 0;
                    try
                    {
                        id = Convert.ToInt32(HttpContext.Current.Request["idem"]);
                    }
                    catch { }
                    strUri = Global.ServiceUriDictionary["itemshape"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    itemloc = JsonConvert.DeserializeObject<ItemLocation>(stringReader.ReadToEnd());
                }

                itemloc.PostHandler<HISModels.Entities.ItemLocation>();
                strUri = Global.ServiceUriDictionary["itemlocation"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true")
                {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(itemloc));
                }
                else
                {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(itemloc));
                }
                string temp = stringReader.ReadToEnd();


                itemloc = JsonConvert.DeserializeObject<ItemLocation>(temp);

                if (itemloc != null)
                {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }
                else
                {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            }
            catch (Exception ex)
            {
                //strNotification = "{\"strNotification\": \"" + ex.Message + "\"}";
                HttpContext.Current.Response.Write(ex.Message);
            }
            finally
            {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-items")]

        public void CreateItemLocation()
        {
            StringReader stringReader = null;
            try
            {
                Data["Pagetitle"] += " :: Add Item Shapes ::";
                itemloc = new HISModels.Entities.ItemLocation() { NameValue = string.Empty, ItemSiteId = 0 };
                Data.Add("refItemShape", itemloc);
                Data.Add("IsCreate", "true");
                Data["Notification"] = strNotification;

            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {
                RenderFile("Master\\itemgroups\\edititemlocation.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        #endregion



    }
}